﻿@{
}
<section class="container-fluid m15_t">
    <div class="data-wrap case">
        <div class="data-tool clearfix">
            <div class="pull-left form-inline ">
                <a class="btn btn-default btn-sm export m10_l" href="javascript:void(0)" data-title="导出">导出</a>
                <div class="input-group input-group-sm">
                    <label class="input-group-addon m5_l">收款余额：<b>@(ViewBag.DepositBalance)</b></label>
                </div>
                <div class="input-group input-group-sm">
                    <label class="input-group-addon m5_l">收款冻结余额：<b>@(ViewBag.DepositProxyFrozenBalance)</b></label>
                </div>
                <div class="input-group input-group-sm">
                    <label class="input-group-addon m5_l">下发余额：<b>@(ViewBag.PaymentBalance)</b></label>
                </div>
                <div class="input-group input-group-sm">
                    <label class="input-group-addon m5_l">下发冻结余额：<b>@(ViewBag.PaymentProxyFrozenBalance)</b></label>
                </div>
                @{
                    if (ViewBag.MerchantRecharge == "MerchantRechargel0001")
                    {
                        <div class="input-group input-group-sm">
                            <a class="btn btn-default btn-sm" data-modal="/Business/Recharge" data-title="内充" data-confirmname="确认" data-width="430" data-height="250"><span class="glyphicon glyphicon-plus"></span> 内充</a>
                        </div>
                    }
                }
                @{
                    if (ViewBag.MerchantWithdrawal == "MerchantWithdrawal0001")
                    {
                        <div class="input-group input-group-sm">
                            <a class="btn btn-default btn-sm" data-modal="/Business/Withdrawal" data-title="提现" data-confirmname="申请" data-width="430" data-height="350"><span class="glyphicon glyphicon-minus"></span> 提现</a>
                        </div>
                    }
                }
            </div>
            <div class="pull-right">
                <form class="form-inline" id="formSearch">
                    <select name="logSource" class="form-control">
                        <option value="">请选择</option>
                        @{
                            foreach (var dict in (IDictionary<int, string>)ViewBag.BalanceLogSource)
                            {
                                                <option value="@dict.Key" )>@dict.Value</option>
                            }
                        }
                    </select>
                    <input id="d421" class="Wdate wd175" name="startTime" type="text" value="@(ViewBag.StartTime)" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',maxDate:'%y-%M-%d'})" autocomplete="off" />
                    -
                    <input id="d422" class="Wdate wd175 m5_l" name="endTime" type="text" value="@(ViewBag.EndTime)" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',minDate:'#F{$dp.$D(\'d421\')}'})" autocomplete="off" />
                    <button type="button" class="btn btn-default btn-sm active gettext" id="btnSearch">搜索</button>
                </form>
            </div>
        </div>
        <div class="paging">
        </div>
    </div>
</section>
@section footerScript{
    <script type="text/javascript">
        $(function () {
            FlashPay.UI.DataAutoHeight();

            doSearch();

            $("#btnSearch").click(function () {
                doSearch();
            });
        });

        function Tip_success(word) {
            FlashPay.UI.Tip_success(word);
        }

        function doSearch() {
            FlashPay.UI.Pager(".paging", { ajax: { url: '@Url.Action("BalanceQuery", "Business")' } });
        }

        $(".export").click(function () {
            FlashPay.UI.Confirm({
                content: "下载余额记录需要花费大量时间,确认要下载吗？",
                ok: function () {

                    var cover = FlashPay.UI.Mask({
                        obj: $("body"),
                        opacity: 0.5
                    });

                    var query = $("#formSearch").serializeJSON();


                    FlashPay.Util.Ajax({
                        url: '/Business/BalanceExport',
                        type: 'POST',
                        data: { query: query },
                        success: function (response) {
                            if (response.success) {
                                cover.Remove();
                                if (response.rows != null && response.rows.length > 0) {
                                    var body = '<table>';

                                    body += '<tr>';
                                    body += '<td>编号</td>';
                                    body += '<td>来源</td>';
                                    body += '<td>金额</td>';
                                    body += '<td>之前余额</td>';
                                    body += '<td>之后余额</td>';
                                    body += '<td>创建时间</td>';
                                    body += '<td>备注</td>';
                                    body += '</tr>';


                                    $.each(response.rows, function (i, item) {

                                        var source = '';
                                        var sources = [
                                            { value: 10, Text: '<span class="bold">存款余额</span>' },
                                            { value: 20, Text: '<span class="green bold">存款余额冻结</span>' },
                                            { value: 30, Text: '<span class="red bold">付款余额</span>' },
                                            { value: 40, Text: '<span class="bold">付款余额冻结</span>' },
                                            { value: 45, Text: '<span class="bold">预付</span>' },
                                            { value: 50, Text: '<span class="bold">充值</span>' },
                                            { value: 55, Text: '<span class="bold">扣除代付余额</span>' },
                                            { value: 60, Text: '<span class="bold">收款撤销</span>' },
                                            { value: 70, Text: '<span class="bold">代付撤销</span>' },
                                            { value: 75, Text: '<span class="bold">代付失败</span>' },
                                            { value: 80, Text: '<span class="bold">结算</span>' }
                                        ];

                                        var sourceFindResult = sources.find(x => { return x.value === item.source; });
                                        if (sourceFindResult) {
                                            source += sourceFindResult.Text;
                                        }

                                        var type = '';
                                        if (item.type == 10) {
                                            type += '+';
                                        } else if (item.type == 20) {
                                            type = '-';
                                        }


                                        body += '<tr>';
                                        body += '<td>' + item.id + '</td>';
                                        body += '<td>' + source + '</td>';
                                        body += '<td>' + type + item.amount + '</td>';
                                        body += '<td>' + item.beforeBalance + '</td>';
                                        body += '<td>' + item.afterBalance + '</td>';
                                        body += '<td>' + item.createdDate + '</td>';
                                        body += '<td>' + item.remark + '</td>';
                                        body += '</tr>';
                                    });

                                    body += '</table>';

                                    var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>";
                                    excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">';
                                    excelFile += "<head>";
                                    excelFile += "<!--[if gte mso 9]>";
                                    excelFile += "<xml>";
                                    excelFile += "<x:ExcelWorkbook>";
                                    excelFile += "<x:ExcelWorksheets>";
                                    excelFile += "<x:ExcelWorksheet>";
                                    excelFile += "<x:Name>";
                                    excelFile += "Sheet1";
                                    excelFile += "</x:Name>";
                                    excelFile += "<x:WorksheetOptions>";
                                    excelFile += "<x:DisplayGridlines/>";
                                    excelFile += "</x:WorksheetOptions>";
                                    excelFile += "</x:ExcelWorksheet>";
                                    excelFile += "</x:ExcelWorksheets>";
                                    excelFile += "</x:ExcelWorkbook>";
                                    excelFile += "</xml>";
                                    excelFile += "<![endif]-->";
                                    excelFile += "</head>";
                                    excelFile += "<body>";
                                    excelFile += body;
                                    excelFile += "</body>";
                                    excelFile += "</html>";

                                    var uri = window.URL.createObjectURL(new Blob([excelFile], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }));
                                    var link = document.createElement("a");
                                    link.href = uri;

                                    link.style = "visibility:hidden";
                                    link.download = response.statusCode + ".xls";

                                    document.body.appendChild(link);
                                    link.click();
                                    document.body.removeChild(link);
                                }
                                else {
                                    cover.Remove();
                                    FlashPay.UI.Tip_short_warning("暂无记录");
                                }

                            } else {
                                cover.Remove();
                                FlashPay.UI.Tip_short_warning("操作失败！" + response.errorMessage);
                            }
                        },
                        error: function (json) {
                            cover.Remove();
                            FlashPay.UI.Tip_warning("操作失败！请查看网络，请重试。");
                        }
                    });
                }
            });
        })

    </script>
}


